package com.yueya.business.reservation;

import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;

@SuppressWarnings("serial")
public class Reservation extends Model<Reservation> {

	public static Reservation dao = new Reservation();

	public boolean save(Reservation dao) {
		dao.set("reservation_id", UUIDGenerator.getUUID());
		dao.set("registrant_date", DateUtil.getDate(DateUtil.timeFormat));
		return dao.save();
	}

	public boolean update(Reservation dao) {
		dao.set("registrant_date", DateUtil.getDate(DateUtil.timeFormat));
		return dao.update();
	}

	public Page<Record> paginate(int pageNumber, int pageSize, Map<String, String> map) {
		String select = "select reserv.reservation_id reservationid,reserv.reservation_name reservationname,reserv.reservation_txt reservationtxt,reserv.reservation_type reservationtype,reserv.contact_number contactnumber,reserv.senior_name seniorname,reserv.senior_age seniorage";
		StringBuffer sql = new StringBuffer(" from jg_reservation reserv where del_flag='0' ");

		for (Map.Entry<String, String> entry : map.entrySet()) {
			if (entry.getValue().contains("%")) {
				sql.append(" and reserv." + entry.getKey() + "like '" + entry.getValue() + "'  ");
			} else if (StrKit.notBlank(entry.getValue())) {
				sql.append(" and reserv." + entry.getKey() + " ='" + entry.getValue() + "'  ");
			}
		}

		if (StrKit.notBlank(map.get("order"))) {
			sql.append(" order by  " + map.get("order"));
		} else {
			sql.append(" order by reserv.registrant_date desc");
		}

		return Db.paginate(pageNumber, pageSize, select, sql.toString());
	}

}
